﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Public Class FormMesWorkReportList
    Dim objPgdMesWorkReport As New clsPgdMesWorkReport
    Private objPgdMes As New clsPgdMes
    Dim httpHelper As New HttpHelper
    Dim txt As TextBox
    Private Sub FormMesWorkReportList_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim startDate As String = Date.Now.ToShortDateString()
        txtStartDate.Text = startDate
        Dim endDate As String = Date.Now.ToShortDateString()
        txtEndDate.Text = endDate
        setDgvData()
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        setDgvData()
    End Sub

    Private Sub setDgvData()
        Dim dt As DataTable = objPgdMesWorkReport.getList(txtStartDate.Text & " 00:00:00.000", txtEndDate.Text & " 23:59:59.999")
        If dt.Rows.Count > 0 Then
            dt.Columns.Add("validity_name")
            For Each row As DataRow In dt.Rows
                If row("validity") = "0" Then
                    row("validity_name") = "无效"
                Else
                    row("validity_name") = "有效"
                End If
            Next
        End If
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dt
    End Sub

    Private Sub btnMesWorkReport_Click(sender As Object, e As EventArgs) Handles btnMesWorkReport.Click
        If dgv.SelectedRows.Count > 0 Then
            Dim prod_id As String = dgv.SelectedRows(0).Cells("Col_prod_id").Value.ToString()
            Dim PgdMesSysId As String = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
            'objPgdMesWorkReport.insert("ok", PgdMesSysId, "1", "力学", prod_id, "4", "0")
            Dim dtPgdMes As DataTable = objPgdMes.getListBySysId(PgdMesSysId)
            Dim b_PgdMesProjectCode As String = dtPgdMes.Rows(0)("mes_project_code").ToString()
            Dim b_PgdMes_gx As String = dtPgdMes.Rows(0)("type_value").ToString()
            Dim rtn_mes_bg As String = mesbg("1", b_PgdMes_gx, b_PgdMesProjectCode)
            Dim jo As JObject = JsonConvert.DeserializeObject(rtn_mes_bg)
            Dim code As String = jo("code").ToString() '输出0
            Dim msg As String = jo("msg").ToString() '输出操作成功
            'objPgdMesWorkReport.insert("ok", PgdMesSysId, "1", "力学", prod_id, "4", code)
            If code.Equals("0") Then
                MsgBox("保存成功，MES报工成功", vbYes, "提示")
            Else
                MsgBox("保存成功，MES报工失败：" & msg, vbYes, "提示")
            End If
        End If

    End Sub

    Private Function mesbg(oper_num As String, productorname As String, b_gtPgdMesProjectCode As String) As String
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """qualifiednumber"":""" & Double.Parse(oper_num) & """," '报工的合格数
        params &= """unqualifiednumber"":""" & 0.00 & """," '报工的不合格数
        params &= """productorname"":""" & productorname & """," '报工产品编码
        params &= """procedurename"":""" & "力学" & """," '报工工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """unqualifiedReason"":""" & "" & """" '不合格原因
        params &= "}"
        Return HttpHelper.HttpPostWebService("dispatchingOrderBG", params)
    End Function

    Private Sub txt_MouseClick(sender As Object, e As MouseEventArgs) Handles txtStartDate.MouseClick, txtEndDate.MouseClick
        txt = CType(sender, TextBox)
        DateNavigator1.Left = txt.Left
        DateNavigator1.Top = txt.Location.Y + txt.Height
        DateNavigator1.Visible = True
    End Sub

    Private Sub DateNavigator1_DateTimeChanged(sender As Object, e As EventArgs) Handles DateNavigator1.DateTimeChanged
        txt.Text = DateNavigator1.DateTime.ToShortDateString()
        DateNavigator1.Visible = False
    End Sub

End Class